package com.celesea.workflow.collaboration.service;

import com.celesea.framework.service.AbstractBaseService;
import com.celesea.workflow.collaboration.dao.CollaborationDao;
import com.celesea.workflow.collaboration.entity.Collaboration;
import com.celesea.workflow.collaboration.vo.FlowVo;
import com.celesea.workflow.core.dao.WorkflowNoticeDao;
import com.celesea.workflow.core.entity.WorkflowDefine;
import com.celesea.workflow.core.entity.WorkflowInstance;
import com.celesea.workflow.core.entity.WorkflowType;
import com.celesea.workflow.core.service.WorkflowDefineService;
import com.celesea.workflow.core.service.WorkflowInstanceService;
import com.celesea.workflow.core.service.WorkflowNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author JackYuan
 * @create 2017-11-18 15:40
 */
@Service
public class CollaborationServiceImpl extends AbstractBaseService<Collaboration> implements CollaborationService {

    public static final String CODE = "000001";
    @Autowired
    private CollaborationDao collaborationDao;
    @Autowired
    private WorkflowDefineService workflowDefineService;
    @Autowired
    private WorkflowInstanceService workflowInstanceService;

    @Autowired
    private WorkflowNoticeDao workflowNoticeDao;
    @Autowired
    private WorkflowNoticeService workflowNoticeService;


    @Override
    public Collaboration createWorkflow(Collaboration collaboration) {
        Collaboration entity = super.insert(collaboration);

        WorkflowDefine define = workflowDefineService.findActive(CODE);
        WorkflowInstance instance = new WorkflowInstance(define, entity);
        instance.setWfType(WorkflowType.COLLABORATION.getCode());
        instance.setWfDataId(entity.getId());
        instance.setActiveNodeId(define.getStartNodeId());
        instance.setSummary(entity.getTaskName());
        workflowInstanceService.createWokflowInstance(instance);

        return entity;
    }

    @Override
    public void updateFlow(FlowVo flowVo) {
        //更新表单数据
        Collaboration entity = super.update(new Collaboration(flowVo));
        // TODO  过滤可更新权限
        workflowInstanceService.updateWokflowInstance(flowVo.getFlow(), entity);
    }

}
